package com.atguigu.gulimall.auth.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.common.vo.MemberRespVO;
import com.atguigu.gulimall.auth.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@RequestMapping("/oauth2.0")
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;


    //请求用户授权Token
    @GetMapping("/weiboAuthorize")
    public String weiboAuthorize(){
        //拼装授权链接
        return "https://api.weibo.com/oauth2/authorize?client_id=1413076064&response_type=code&redirect_uri=http://auth.gulimall.com/oauth2.0/weibo/success";
    }

    // TODO: 待处理：社交账号第一次登录会失败
    //用户授权通过,获取授权过的Access Token
    @GetMapping("/weibo/success")
    public String weiboAccessToken(@RequestParam("code") String code, HttpSession session){
        //https://api.weibo.com/oauth2/access_token?client_id=1413076064&client_secret=a9373e2538535f6a233731e3dd7d6946&grant_type=authorization_code&redirect_uri=http://gulimall.com/oauth2.0/weibo/success&code=5b1b58df7d2655bb3d091bfe6b7dbd43
        HashMap<String, Object> param = new HashMap<>();
        param.put("client_id", "1413076064");
        param.put("client_secret", "a9373e2538535f6a233731e3dd7d6946");
        param.put("grant_type", "authorization_code");
        param.put("redirect_uri", "http://auth.gulimall.com/oauth2.0/weibo/success");
        param.put("code", code);
        //1、根据code获取accessTaken
        String resultString = HttpUtil.post("https://api.weibo.com/oauth2/access_token", param);
        //{"error":"invalid_request","error_code":21323,"request":"/oauth2/access_token","error_uri":"/oauth2/access_token","error_description":"miss client id or secret"}
        //{"access_token":"2.00eX8rjF7dHdXBcbd90a4577LJiQ9B","remind_in":"157679999","expires_in":157679999,"uid":"5258336538","isRealName":"true"}
        System.out.println(resultString);
        JSONObject result = JSON.parseObject(resultString);
        if (result.get("error") != null) {
            return result.getString("error");
        }
        SocialUser socialUser = JSON.parseObject(resultString, SocialUser.class);
        R r = memberFeignService.oauthLogin(socialUser);
        if (r.getCode() == 0) {
            MemberRespVO respVO = r.getData("data", new TypeReference<MemberRespVO>() {});
            System.out.println("成功登录 --- "+respVO);
            //存入session
            session.setAttribute(AuthServerConstant.LOGIN_USER,respVO);
            return "redirect:http://gulimall.com";
        }
        return "redirect:http://auth.gulimall.com/login.html";
    }

}
